﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Texfina.Core.Common;
using Texfina.Core.Web;
using Texfina.Common.Mg;
using Texfina.Core.Data;
using Texfina.Entity.Rh;
using Texfina.Entity.Mg;
using RHBOQry = Texfina.BOQry.Rh;
using RHBOMnt = Texfina.BOMnt.Rh;
using MGBOQry = Texfina.BOQry.Mg;
using System.Text;

namespace RH
{
   public partial class wfProcPlaCalcBnd : SearchPage
   {

      public const int _GRDLIST_STPROCESO = 9;
      public const int _GRDLIST_STANULADO = 10;

      protected void Page_Load(object sender, EventArgs e)
      {

         this.LoadInitialSettings();
         if (this.IsPostBack)
         {
            this.LoadInPostBack();
            return;
         }

         this.LoadData();

      }

      public override void LoadInitialSettings()
      {
         Label lbl = (Label)this.Master.FindControl("lblTitle");
         lbl.Text = "Calculo de planillas";
         this.Page.Form.DefaultButton = bttSearch.UniqueID;
      }

      public override void LoadListControls()
      {
         ETablaGenDet objEDet = new ETablaGenDet();
         MGBOQry.ITablaGen objBOI = (MGBOQry.ITablaGen)WCFHelper.GetObject<MGBOQry.ITablaGen>(typeof(MGBOQry.TablaGen));

         objEDet.IdTabla = ConstMultitabla.MES;
         DataTable dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidMes, dt, "dsDescrip", "idCodigo", true, true);

         objEDet.IdTabla = ConstMultitabla.FORMA_PAGO;
         dt = objBOI.GetDetailByList(objEDet);
         WebHelper.LoadListControl(ddlidForPago, dt, "dsDescrip", "idCodigo", true, true);

      }

      public override void SearchData()
      {
         EProcPlaniBandeja objE = new EProcPlaniBandeja();

         objE.IdPeriodo = Session[ConstSessionVar.PERIODO].ToString();
         objE.IdEmpresa = Session[ConstSessionVar.EMPRESA].ToString();

         if (this.ddlidMes.SelectedValue.ToString() == "")
            objE.IdMes = null;
         else
            objE.IdMes = this.ddlidMes.SelectedValue.ToString();

         if (this.ddlidForPago.SelectedValue.ToString() == "")
            objE.IdForPago = null;
         else
            objE.IdForPago = this.ddlidForPago.SelectedValue.ToString();

         if (this.txtidPlanilla.Text == "")
            objE.IdPlanilla = null;
         else
            objE.IdPlanilla = this.txtidPlanilla.Text;

         if (this.txtdsPlanilla.Text == "")
            objE.DsPlanilla = null;
         else
            objE.DsPlanilla = this.txtdsPlanilla.Text;

         IBOQuery objBO = (IBOQuery)WCFHelper.GetObject<IBOQuery>(typeof(RHBOQry.ProcPlani));
         DataTable dt = objBO.GetByCriteria(objE);

         this.GridSetDataBinding(grdList, dt);

      }

      public void RegisterEditPopupScripts()
      {
         StringBuilder sb = new StringBuilder();

         sb.AppendLine("var varRet = modalDialog('../RH/wfPersonalPlani.aspx?IdPlanilla=' + document.getElementById('" + this.hdnidPlanilla.ClientID + "').value" + ",725,400);");
         sb.AppendLine("if(varRet==null)");
         sb.AppendLine("return false;");
         sb.AppendLine("ctl = document.getElementById('" + this.hdnidPersonal.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[0];");
         sb.AppendLine("ctl = document.getElementById('" + this.txtidPersonal.ClientID + "');");
         sb.AppendLine("ctl.value = varRet[1];");
         sb.AppendLine("return false;");

         this.txtidPersonal.OnClientClick = sb.ToString();

         sb = new StringBuilder();

         sb.AppendLine("function validate" + this.bttOkPopup.ID + "(){");
         sb.AppendLine("ctl = document.getElementById('" + this.rbExeOne.ClientID + "');");
         sb.AppendLine("if(ctl.checked){");
         sb.AppendLine("varRpta = isEmptyControl('" + this.txtidPersonal.ClientID + "','Seleccione el Personal.');");
         sb.AppendLine("if(varRpta==true)");
         sb.AppendLine("return !varRpta;");
         sb.AppendLine("}");
         sb.AppendLine("return true;}");

         ScriptManager.RegisterStartupScript(this, this.GetType(), "validatePopup", sb.ToString(), true);

         this.bttOkPopup.OnClientClick = "return validate" + bttOkPopup.ID + "();";
         this.bttCancelPopup.OnClientClick = "$find('" + this.mdlPopup.ClientID + "').hide();return false;";

      }

      protected void grdList_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
         RegisterEditPopupScripts();

         txtidPersonal.Text = "";
         hdnidPersonal.Value = "";
         rbExeTodos.Checked = true;
         rbExeOne.Checked = false;

         DataRow dr = this.GridGetRowFromDataKey(grdList, Convert.ToInt32(e.CommandArgument));
         hdnidPeriodo.Value = dr["idPeriodo"].ToString();
         hdnidEmpresa.Value = dr["idEmpresa"].ToString();
         hdnidForPago.Value = dr["idForPago"].ToString();
         hdnidPlanilla.Value = dr["idPlanilla"].ToString();
         hdnnuProcAnual.Value = dr["nuProcAnual"].ToString();
         hdnidMes.Value = dr["idMes"].ToString();

         this.lblTitlePopup.Text = dr["dsForPago"].ToString() + " - " + dr["dsMes"].ToString() + " - " + dr["dsPlanilla"].ToString() + " - " + dr["nuProcAnual"].ToString();

         this.mdlPopup.Show();


      }

      protected void bttOkPopup_Click(object sender, EventArgs e)
      {
         ProcesarPlanilla();

      }
      protected void grdList_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
            String strRI = e.Row.RowIndex.ToString();

            ImageButton bttedit = (ImageButton)e.Row.FindControl("grdBtt" + ConstGridRowCommand.GRIDEDIT);

            bttedit.OnClientClick = "javascript:return validateGridCommands('" + ConstGridRowCommand.GRIDEDIT + "','" + strRI + "');";

            if (e.Row.Cells[_GRDLIST_STANULADO].Text == ConstFlagEstado.ACTIVADO)
            {
               e.Row.ForeColor = System.Drawing.Color.Red;
               bttedit.ImageUrl = "../Images/Icons/Disabled/DbStart.png";
            }
            else
            {
               if (e.Row.Cells[_GRDLIST_STPROCESO].Text == ConstEstadoProcePlani.CERRADA)
               {
                  e.Row.ForeColor = System.Drawing.Color.Blue;
                  bttedit.ImageUrl = "../Images/Icons/Disabled/DbStart.png";
               }
            }
         }
      }


      protected void bttOk_Click(object sender, EventArgs e)
      {
         this.SearchData();
      }

      private void ProcesarPlanilla()
      {

         RHBOMnt.IProcPlani objBO = WCFHelper.GetObject<RHBOMnt.IProcPlani>(typeof(RHBOMnt.ProcPlani));
         EProcPlaniProcCalc objE = new EProcPlaniProcCalc();

         objE.IdPeriodo = hdnidPeriodo.Value;
         objE.IdEmpresa = hdnidEmpresa.Value;
         objE.IdForPago = hdnidForPago.Value;
         objE.IdPlanilla = hdnidPlanilla.Value;
         objE.NuProcAnual = Convert.ToInt32(hdnnuProcAnual.Value.ToString());
         objE.IdMes = hdnidMes.Value;

         if (rbExeOne.Checked)
            objE.IdPersonal = hdnidPersonal.Value;
         else
            objE.IdPersonal = null;
         
         objBO.ProcesarCalculo(objE);

         string strMsg = "Se proceso correctamente la Planilla seleccionada";
         this.hdnMsgOPB.Value = this.CreatePostbackMessage(0, strMsg, "");

      }
   }
}